﻿package tools;

public class TryCatchDemo {
	private int x = 0;

	public static void main(String[] args) {
		TryCatchDemo tcd = new TryCatchDemo();
		int i = tcd.funone();
		System.out.println("i=" + i);
		//方法一执行完后,对象中x的值为1,所以当调用完方法二后x为2
		i = tcd.funtwo();
		System.out.println("i=" + i);
	}

	public int funone() {
		try {
			return x;
		} catch (Exception e) {
			System.out.println("~~~Exception happened!");
		} finally {
			// 将在try块中的return语句执行中间执行
			++x;
		}
		return ++x;
	}

	@SuppressWarnings("finally")
	public int funtwo() {
		try {
			return x;
		} catch (Exception e) {
			System.out.println("~~~Exception happened!");
		} finally {
			/*
			 * 虽然try块中已经有return了，但是不会马上返回 只是放入栈中，等待finally执行完成
			 */
			return ++x;
		}
	}
}
